package net.chanibal.hala.client;



import net.chanibal.hala.client.Server.KlientData;
import net.chanibal.hala.client.Server.TerminData;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.IncompatibleRemoteServiceException;
import com.google.gwt.user.client.rpc.InvocationException;
import com.google.gwt.user.client.rpc.SerializableException;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class Klient extends DialogBox implements AsyncCallback {
    private Label label;
    private VerticalPanel verticalPanel;
    String login;
    
    public Klient(String login) {
    		this.login=login;
	      // Set the dialog box's caption.
	      setText("Klient "+login);

	      // DialogBox is a SimplePanel, so you have to set its widget property to
	      // whatever you want its contents to be.
	      
	      setWidget(new Label("Wczytywanie danych"));
	      Server.Util.getInstance().getKlient(login, this);

	      center();
	      
  }
    public void setError(String s)
    {
		VerticalPanel v = new VerticalPanel();
		final Label label = new Label(s);
		label.setStyleName("gwt-Label-Error");
		v.add(label);
		v.add(new Button("Zamknij",new ClickListener(){

			
			public void onClick(Widget sender) {
				hide();
			}
			
		}));
		setWidget(v);
		center();
    }
    
	public void onFailure(Throwable caught) {
		String s;
		caught.printStackTrace();
		try {
		      throw caught;
		} catch (IncompatibleRemoteServiceException e) {
		  // this client is not compatible with the server; cleanup and refresh the 
		  // browser
			s=e.toString();
		} catch (InvocationException e) {
		  // the call didn't complete cleanly
			s="Błąd połączenia:"+e.toString();
		 e.printStackTrace();
		} catch (SerializableException e) {
		  // one of the 'throws' from the original method
			s=e.getMessage();
		} catch (Throwable e) {
		  // last resort -- a very unexpected exception
			s=e.getLocalizedMessage();
		}
		setError(s);
	}

	class WplacilForm extends AbstractForm implements AsyncCallback{

		TextField kasa = new TextField(":","[1-9][0-9]*([.][0-9][0-9])?");
		
		WplacilForm() {
			super("Wpłata");
			addField(kasa);
		}

		public void doRequest()
		{
			Server.Util.getInstance().doWplata(login, Double.valueOf(kasa.getText()), this );
			
		}

		
		public void onFailure(Throwable caught) {
			// TODO Auto-generated method stub
			setError(caught.getLocalizedMessage());
		}

		
		public void onSuccess(Object result) {
			// TODO Auto-generated method stub
			History.onHistoryChanged(History.getToken());
			hide();
		}
		
	}
	
	public void onSuccess(Object result) {
		  KlientData data = (KlientData) result;
	      verticalPanel = new VerticalPanel();
	      setWidget(verticalPanel);
	      verticalPanel.setSize("100%", "100%");

	      //new Label("New Label"+data.name);
	      
	 
	      verticalPanel.add(new Label("Imię:"+data.name));
	      verticalPanel.add(new Label("Nazwisko:"+data.familyName));
	      verticalPanel.add(new Label("Rok urodzenia:"+data.age));
	      verticalPanel.add(new Label("Płeć:"+(data.sex.equals("K")?"Kobieta":"Mężczyzna")));
	      verticalPanel.add(new HTML("<hr/>"));
	      if(data.terminy.length>0){
		      verticalPanel.add(new Label("Zarezerwowane Terminy:"));
		      for(int x=0;x<data.terminy.length;x++){
		    	  TerminData t = data.terminy[x];
		    	  verticalPanel.add(new Label(t.nazwaImprezy+" ("+t.rozpoczecie.toLocaleString()+" - "+t.zakonczenie.toLocaleString()+")"));
		      }
	      }else{
	    	  verticalPanel.add(new Label("Brak Rezerwacji"));
	      }
	      verticalPanel.add(new HTML("<hr/>"));
	      verticalPanel.add(new Label("Saldo:"+data.bill));
	      verticalPanel.add(new WplacilForm());
	      verticalPanel.add(new HTML("<hr/>"));
	      verticalPanel.add(new Button("Zamknij",new ClickListener(){
				public void onClick(Widget sender) {
					hide();
				}
			}));
	      center();
	}
	      
	      
}